fn binary_search<T:PartialOrd>(v: &[T], searchvalue: T) -> Option<usize> {
    let mut lower = 0 as usize;
    let mut upper = v.len() - 1;

    while upper >= lower {
        let mid = (upper + lower) / 2;
        if v[mid] == searchvalue {
            return Some(mid);
        } else if searchvalue < v[mid] {
            upper = mid - 1;
        } else {
            lower = mid + 1;
        }
    }

    None
}

fn main() {
    let v = [-31, 0, 1, 2, 2, 4, 65, 83, 99, 782];
    let x = 2;
    if let Some(value) = binary_search(&v, x) {
        println!("{} is at index {}.", x, value);
    } else {
        println!("{} is not found.",x);
    }
}
